package hk.m4s.lr.repair.test.ui.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.itheima.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.List;

import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.ImageModle;
import hk.m4s.lr.repair.test.model.SelectCouponModel;
import hk.m4s.lr.repair.test.model.enValueModel;
import hk.m4s.lr.repair.test.views.InnerGridView;

/**
 * Created by qi.wang on 2017/3/16.
 */

public class SlectCouponAdapter extends BaseAdapter {
    public List<SelectCouponModel> list = new ArrayList<>();
    Context context;
    LayoutInflater inflater;
    public SlectCouponAdapter(Context context, List<SelectCouponModel> data) {
        this.context = context;
        this.list = data;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item_select_coupon, parent, false);
            holder.couponName = (TextView) convertView.findViewById(R.id.couponName);
            holder.couponTime = (TextView) convertView.findViewById(R.id.couponTime);
            holder.couponPrice = (TextView) convertView.findViewById(R.id.couponPrice);
            holder.couponRule = (TextView) convertView.findViewById(R.id.couponRule);
            holder.couponTips= (TextView) convertView.findViewById(R.id.couponTips);
            holder.showRlue = (RelativeLayout) convertView.findViewById(R.id.showRlue);
            holder.showTips = (RelativeLayout) convertView.findViewById(R.id.showTips);
            holder.couponUp=(ImageView)  convertView.findViewById(R.id.couponUp);
            holder.couponSelect=(TextView) convertView.findViewById(R.id.couponSelect);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.showRlue.setTag(position);
        holder.couponSelect.setTag(position);
        SelectCouponModel newsModel = list.get(position);
        holder.couponName.setText(newsModel.getCouponName());
        holder.couponTime.setText(newsModel.getStartTime()+"至"+newsModel.getEndTime()+"有效");
        holder.couponRule.setText(newsModel.getCouponTips());
        holder.couponPrice.setText(newsModel.getCouponPrice());
        holder.couponTips.setText(newsModel.getCouponRule());
        if(newsModel.isShow()){
            holder.showTips.setVisibility(View.VISIBLE);
            holder.couponUp.setImageResource(R.mipmap.down);
        }else{
            holder.showTips.setVisibility(View.GONE);
            holder.couponUp.setImageResource(R.mipmap.up);
        }
        if(newsModel.getCheck().equals("0")){
            Drawable drawable = context.getResources().getDrawable(R.mipmap.couponcheck);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            holder.couponSelect.setCompoundDrawables(drawable, null, null, null);
        }else{
            Drawable drawable = context.getResources().getDrawable(R.mipmap.couponcheckd);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            holder.couponSelect.setCompoundDrawables(drawable, null, null, null);
        }
        holder.showRlue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num=(Integer) v.getTag();
                SelectCouponModel model=list.get(num);
                if(model.isShow()){
                    model.setShow(false);
                }else{
                    model.setShow(true);
                }
                list.set(num,model);
                notifyDataSetChanged();
            }
        });
        holder.couponSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num=(Integer) v.getTag();
                for(int i=0;i<list.size();i++){
                    SelectCouponModel model=list.get(i);
                    if(i==num){
                        if(model.getCheck().equals("0")) {
                            model.setCheck("1");
                        }else{
                            model.setCheck("0");
                        }
                    }else{
                        model.setCheck("0");
                    }
                    list.set(i,model);
                }
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    public static class ViewHolder {
        TextView couponName, couponTime, couponPrice, couponRule,couponTips,couponSelect;
        ImageView couponUp;
        RelativeLayout showRlue;
        RelativeLayout showTips;
    }




}
